#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from enum import Enum
from common.env_common import get_install_head_path


class CommandReturnCode(Enum):
    ERROR = -1
    # plugin or proxy framework executes successfully
    SUCCESS = 0
    # plugin or proxy framework executes successfully, client need to perform this operation continue
    CONTINUE = 100
    # plugin or proxy framework is busy, client should perform this operation after a period of time
    BUSY = 101
    # an internal error occurred in the plugin or proxy framework
    INTERNAL_ERROR = 200
    KERBEROS_ERROR = 0x5E0250FC


class KerberosConstant(Enum):
    KRB5_CONF = "krb5Conf"
    CONFIG_MODEL = 'configModel'
    KEYTAB_MODEL = 'keytab_model'
    PASSWORD_MODEL = 'password_model'
    KEYTAB = 'keytab'
    SECRET = 'secret'
    KINIT_FAILED = 'kinit failed'
    DOES_NOT_EXIST = "doesn't exist"
    TABLE_DOES_NOT_EXIST = "Table doesn't exist"


class KerberosCmdRetKey(Enum):
    CLIENT_NOT_FOUND = 'not found'
    INFORMATION_ERROR = 'information error'
    NO_SUIT_KEY = 'no suitable keys'
    PASSWORD = 'Password'
    EXPIRED = 'expired'
    PREAUTHENTICATION_FAILED = 'Preauthentication failed'
    NO_CREDENTIALS_CACHE = 'No credentials cache'


class PluginPathConstant(Enum):
    TMP_PATH = f'{get_install_head_path()}/DataBackup/ProtectClient/ProtectClient-E/stmp/'
    DB_BIN_PATH = f'{get_install_head_path()}/DataBackup/ProtectClient/Plugins/GeneralDBPlugin/bin/'


class AgentConstant:
    HOST_SN_FILE_PATH = "/etc/HostSN/HostSN"
    SLEEP_TIME = 3
    THREE_DECIMAL_PLACES = 3
    UNITS = {
        1: str('Byte'), 2: str('KB'), 3: str('MB'), 4: str('GB'), 5: str('TB'), 6: str('PB'), 7: str('EB'),
        8: str('ZB'), 9: str('YB')
    }


class TaskStage:
    BACKUP_PREREQUISITE = "BackupPrerequisite"
    BACKUP_GEN_SUB_JOB = "BackupGenSubJob"
    BACKUP = "Backup"


class AccessPermission(Enum):
    RWX_RX_RX = 0o755


class ErrorCode(Enum):
    SUCCESS = 0
    ERROR_NETWORK_CONNECT_TIMEOUT = 0x5E025089  # 1577209993
    ERROR_INTERNAL = 0x5F025101  # 1593987329
    ERR_PLUGIN_CANNOT_BACKUP = 0x64032B0C  # 1677929228
    ERR_EXIST_RUNNING_NODE = 8
    ERR_AOF_ENABLE = 9


class ListAppV2QueryConstant(Enum):
    VERSION = "version"
    DATABASE = 'database'
    TABLE = 'table'


class ExecutePolicy(int, Enum):
    ANY_NODE = 0
    LOCAL_NODE = 1
    EVERY_NODE_ONE_TIME = 2
    RETRY_OTHER_NODE_WHEN_FAILED = 3
    FIXED_NODE = 4


class SubJobType(int, Enum):
    PRE_SUB_JOB = 0
    GENERATE_SUB_JOB = 1
    BUSINESS_SUB_JOB = 2
    POST_SUB_JOB = 3


class JobResult(int, Enum):
    # 任务执行成功
    SUCCESS = 0
    # 任务执行失败
    FAILED = 1
    # 任务中止
    ABORTED = 2


class SqliteServiceField(Enum):
    SQLITE_DATABASE_NAME = 'copymetadata.sqlite'
    SQLITE_DATATABLE_NAME = 'T_COPY_METADATA'
    TYPE_DIR = 'd'
    TYPE_FILE = 'f'


class LogLevel(int, Enum):
    INFO = 1
    WARN = 2
    ERROR = 3
    SERIOUS = 4


class RestoreProgressPhase(int, Enum):
    END_COPY_DATA_TO_PRODUCT = 80
    END_ATTACH_PARTS = 20
    COMPLETE = 100


class BackupType(Enum):
    TABLE = "0"
    TABLE_AND_DATA = "1"
    TABLE_DOES_NOT_EXIST = "-1"


class SysData:
    SYS_STDIN = ''
